package com.mo.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mo.constant.Constants;
import com.mo.mapper.TActivitymapper;
import com.mo.model.TActivity;
import com.mo.model.TUser;
import com.mo.query.ActivityQuery;
import com.mo.service.ActivityService;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author mo
 * version
 */
@Service
public class ActivityServiceImpl implements ActivityService {
    @Resource
    private TActivitymapper tActivitymapper;
    @Override
    public PageInfo<TActivity> getUserByPage(Integer current,ActivityQuery activityQuery) {
        //设置PageHelper
        PageHelper.startPage(current, Constants.PAGE_SIZE);
        //查询
        List<TActivity> list = tActivitymapper.selectUserByPage(activityQuery);
        PageInfo<TActivity> info = new PageInfo<>(list);
        return info;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int saveActivity(ActivityQuery activityQuery) {
        TActivity tActivity = new TActivity();
        BeanUtils.copyProperties(activityQuery, tActivity);
        tActivity.setCreateTime(new Date());
        //security的安全上下文获取
        TUser user = (TUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        tActivity.setCreateBy(user.getId());
        return tActivitymapper.insertSelective(tActivity);
    }

    @Override
    public TActivity getActivityById(Integer id) {
        return tActivitymapper.selectDetailByPrimaryKey(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateActivity(ActivityQuery activityQuery) {
        TActivity tActivity = new TActivity();
        BeanUtils.copyProperties(activityQuery, tActivity);
        tActivity.setEditTime(new Date());
        //security的安全上下文获取
        TUser user = (TUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        tActivity.setEditBy(user.getId());
        return tActivitymapper.updateByPrimaryKeySelective(tActivity);
    }

    @Override
    public int deleteActivityById(Integer id) {
        return tActivitymapper.deleteByPrimaryKey(id);
    }

    @Override
    public int batchDelActivityByIds(List<String> idList) {
        return tActivitymapper.deleteByIds(idList);
    }

    @Override
    public List<TActivity> getOngoingActivity() {
        return tActivitymapper.selectOnGoingActivity();
    }
}
